using UnityEngine;
using System.Collections;

public class ButtonManager : MonoBehaviour 
{
	public Material clickedMaterial;
	Material firstMaterial;
	public string myTag;
	public string nextScene;
	int currentLevel;
	public string answerTag;
	public Material SelectedButton;
		
	void Start () 
	{
		firstMaterial = gameObject.renderer.material;
	}
	
	void Update () 
	{		
		for(int i = 0; i < Input.touches.Length; i++)
		{
			Touch touch = Input.touches[0];
			
			Ray ray = Camera.main.ScreenPointToRay(touch.position);
			RaycastHit hit;
			
			if (Input.touchCount >= 0 && Input.GetTouch(0).phase == TouchPhase.Began)
			{			
				if (collider.Raycast(ray, out hit, 1000) )
				{
					if(answerTag == "Quiz")
					{
						
						
						gameObject.renderer.material = SelectedButton;
					}
					else
					{
						gameObject.renderer.material = clickedMaterial;
					}
				}
			}
			
			if (Input.touchCount >= 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
			{
				if (collider.Raycast(ray, out hit, 1000) )
				{
					gameObject.renderer.material = firstMaterial;
                    print(gameObject.tag);

                    if (hit.transform.tag == "Answer")
                    {
                        hit.transform.GetComponent<QuizManager>().Score("Right");
                    }
					
					if (hit.transform.tag == "WrongAnswer")
                    {
                        hit.transform.GetComponent<QuizManager>().Score("Wrong");
                    }

                    if (hit.transform.tag == "Topic")
                    {
                        Application.LoadLevel(hit.transform.name);
                    }

					if(gameObject.tag == myTag)
					{
						Application.LoadLevel(nextScene);
					}
				}
			}
		}
	}
	
	/*void OnMouseEnter()
	{
		gameObject.renderer.material = clickedMaterial;
	}
	
	void OnMouseExit()
	{
		gameObject.renderer.material = firstMaterial;
	}
	
	void OnMouseDown()
	{
		if(gameObject.tag == tag)
		{
			Application.LoadLevel(nextScene);
		}
	}*/
}
